package org.example.contestb.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.contestb.Mapper.DoctorMapper;
import org.example.contestb.Mapper.MessageMapper;
import org.example.contestb.entity.DTO.BanDTO;
import org.example.contestb.entity.DTO.MessStatusDTO;
import org.example.contestb.entity.DTO.UpdateDoctorDTO;
import org.example.contestb.entity.Doctor;
import org.example.contestb.entity.Message;
import org.example.contestb.entity.Patient;
import org.example.contestb.entity.VO.DoctorVO;
import org.example.contestb.entity.VO.MessageVO;
import org.example.contestb.entity.VO.RoughlyDoctorVO;
import org.example.contestb.entity.VO.RoughlyPatientVO;
import org.example.contestb.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MessageMapper messageMapper;
    @Autowired
    DoctorMapper doctorMapper;
    @Override
    public MessageVO messageDisplay(Integer page,Integer pageSize) {
        Page<Message> messagePage = new Page<>(page, pageSize);
        Page<Message> resultPage = messageMapper.selectPage(messagePage, null);
        return MessageVO.builder()
                .messageList(resultPage.getRecords())
                .total(resultPage.getTotal())
                .build();
    }

    @Override
    public String messageSolving(MessStatusDTO statusDTO) {
        if(!statusDTO.getStatus().equals("已处理")){
            throw new RuntimeException("错误！");
        }

        UpdateWrapper<Message> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",statusDTO.getId()).set("status", statusDTO.getStatus());
        messageMapper.update(null,updateWrapper);

        return "处理成功";
    }

    @Override
    public RoughlyDoctorVO display(Integer page,Integer pageSize) {
        Page<Doctor> doctorPage = new Page<>(page, pageSize);
        Page<Doctor> resultPage = doctorMapper.selectPage(doctorPage, null);
        return RoughlyDoctorVO.builder()
                .doctors(resultPage.getRecords())
                .total(resultPage.getTotal())
                .build();
    }

    @Override
    public List<Doctor> doctorSearch(String name){
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        List<Doctor> doctorList = doctorMapper.selectList(queryWrapper);
        return doctorList;
    }

    @Override
    public String doctorBanning(BanDTO banDTO) {
        UpdateWrapper<Doctor> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",banDTO.getId()).set("status", 0);
        doctorMapper.update(null,updateWrapper);

        return "已封禁";
    }

    @Override
    public String doctorDisbanning(BanDTO banDTO) {
        UpdateWrapper<Doctor> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",banDTO.getId()).set("status", 1);
        doctorMapper.update(null,updateWrapper);

        return "已解封";
    }

    @Override
    public String doctorDelete(BanDTO banDTO){
        if(banDTO.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        doctorMapper.deleteById(banDTO.getId());
        return "删除成功";
    }

    @Override
    public String doctorUpdate(UpdateDoctorDTO updateDoctorDTO) {
        UpdateWrapper<Doctor> updateWrapper = new UpdateWrapper<>();
        if(updateDoctorDTO.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        if(!updateDoctorDTO.getName().isEmpty()){
            updateWrapper.eq("id",updateDoctorDTO.getId()).set("name",updateDoctorDTO.getName());
        }
        if(!updateDoctorDTO.getEmail().isEmpty()){
            updateWrapper.eq("id",updateDoctorDTO.getId()).set("email",updateDoctorDTO.getEmail());
        }
        if(updateDoctorDTO.getRole() != null){
            updateWrapper.eq("id",updateDoctorDTO.getId()).set("role",updateDoctorDTO.getRole());
        }
        doctorMapper.update(null,updateWrapper);
        return "修改成功";
    }
}
